Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web

<System.ComponentModel.DataObject(True)> _
Public Class CategoryService
    Private Shared _provider As CategoryProvider = Nothing
    Private Shared _providers As CategoryCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As CategoryProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As CategoryCollection
        Get
            Return _providers
        End Get
    End Property

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As CategoryConfiguration = CType(ConfigurationManager.GetSection("CategoryProvider"), CategoryConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New CategoryCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(CategoryProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategoryGroups() As Generic.List(Of DataTypes.CategoryGroupItem)
        LoadProviders()
        Return _provider.GetCategoryGroups
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategoryGroups(ByVal SortColumn As String, ByVal SortOrder As String) As Generic.List(Of DataTypes.CategoryGroupItem)
        LoadProviders()
        Return _provider.GetCategoryGroups(SortColumn, SortOrder)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategoryGroups(ByVal SearchColumn As String, ByVal SearchOperator As String, ByVal SearchCriteria As String) As Generic.List(Of DataTypes.CategoryGroupItem)
        LoadProviders()
        Return _provider.GetCategoryGroups(SearchColumn, SearchOperator, SearchCriteria)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert)> _
    Public Shared Sub AddCategoryGroup(ByVal NewCateogryGroup As DataTypes.CategoryGroupItem, ByVal CurrentUser As String)
        LoadProviders()
        _provider.AddCategoryGroup(NewCateogryGroup, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert)> _
    Public Shared Sub AddCategoryGroup(ByVal CategoryGroupDescription As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.AddCategoryGroup(CategoryGroupDescription, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update)> _
    Public Shared Sub EditCategoryGroup(ByVal CategoryGroupId As String, ByVal CategoryGroupItem As DataTypes.CategoryGroupItem, ByVal CurrentUser As String)
        LoadProviders()
        _provider.EditCategoryGroup(CategoryGroupId, CategoryGroupItem, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update)> _
    Public Shared Sub EditCategoryGroup(ByVal CategoryGroupId As String, ByVal CategoryGroupDescription As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.EditCategoryGroup(CategoryGroupId, CategoryGroupDescription, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Delete)> _
    Public Shared Sub DeleteCategoryGroup(ByVal CategoryGroupId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.DeleteCategoryGroup(CategoryGroupId, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategories() As Generic.List(Of DataTypes.CategoryItem)
        LoadProviders()
        Return _provider.GetCategories
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategoriesById(ByVal CategoryGroupId As String) As Generic.List(Of DataTypes.CategoryItem)
        LoadProviders()
        Return _provider.GetCategoriesById(CategoryGroupId)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategoriesByDesc(ByVal CategoryGroupDescription As String) As Generic.List(Of DataTypes.CategoryItem)
        LoadProviders()
        Return _provider.GetCategoriesByDesc(CategoryGroupDescription)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCategories(ByVal SortColumn As String, ByVal SortOrder As String) As Generic.List(Of DataTypes.CategoryItem)
        LoadProviders()
        Return _provider.GetCategories(SortColumn, SortOrder)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
    Public Shared Function GetCateogries(ByVal SearchColumn As String, ByVal SearchOperator As String, ByVal SearchCriteria As String) As Generic.List(Of DataTypes.CategoryItem)
        LoadProviders()
        Return _provider.GetCateogries(SearchColumn, SearchOperator, SearchCriteria)
    End Function

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert)> _
    Public Shared Sub AddCategory(ByVal NewCategory As DataTypes.CategoryItem, ByVal CurrentUser As String)
        LoadProviders()
        _provider.AddCategory(NewCategory, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert)> _
    Public Shared Sub AddCategory(ByVal CategoryGroupId As String, ByVal CategoryShortDescription As String, _
        ByVal CategoryLongDescription As String, ByVal SortOrder As Integer, ByVal ParentCategoryId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.AddCategory(CategoryGroupId, CategoryShortDescription, CategoryLongDescription, SortOrder, ParentCategoryId, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update)> _
    Public Shared Sub EditCategory(ByVal CategoryId As String, ByVal CategoryItem As DataTypes.CategoryItem, ByVal CurrentUser As String)
        LoadProviders()
        _provider.EditCategory(CategoryId, CategoryItem, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update)> _
    Public Shared Sub EditCategory(ByVal CategoryId As String, ByVal CategoryGroupId As String, ByVal CategoryShortDescription As String, _
    ByVal CategoryLongDescription As String, ByVal SortOrder As Integer, ByVal ParentCategoryId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.EditCategory(CategoryId, CategoryGroupId, CategoryShortDescription, CategoryLongDescription, SortOrder, ParentCategoryId, CurrentUser)
    End Sub

    <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Delete)> _
    Public Shared Sub DeleteCategory(ByVal CategoryId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.DeleteCategory(CategoryId, CurrentUser)
    End Sub
End Class
